﻿using DongFangGuoKai.WMSPlatform.Commn;
using DongFangGuoKai.WMSPlatform.Core;
using DongFangGuoKai.WMSPlatform.Domain.Enums;
using DongFangGuoKai.WMSPlatform.Domain.Models;
using SqlSugar;
using System;

namespace DongFangGuoKai.WMS.NodeBussiness.FillEmptyBoxNode
{
    /// <summary>
    /// 空箱入货架
    /// </summary>
    public class PDACheckEmptyBox : NodeBaseBusiness
    {
        public readonly Repository<t_Cell> cellRepository;

        public PDACheckEmptyBox(Repository<t_Cell> context, IServiceProvider provider) : base(provider)
        {
            base.DeviceAction = EmDeviceAction.Transport;
            base.WorkDeviceType = EmWorkDeviceType.AGV;
            base.InstructType = EmInstructType.Move;
            cellRepository = context;
        }
        protected override bool ExecCreateInstruct(out string msg, params object[] data)
        {
            throw new NotImplementedException();

        }

        protected override bool ExecInstructFinished(t_Instruct instruct, out string msg)
        {
            msg = string.Empty;

            string expectedToLocation = instruct.ExpectedToLocation;
            string palletBarcode = instruct.PalletBarcode;
            var palletDal = cellRepository.ChangeRepository<Repository<t_Pallet>>();
            var lockCell = cellRepository.GetFirst(t => t.Code == expectedToLocation);
            if (lockCell == null)
            {
                msg = "仓位不存在";
                return false;
            }
            var pallet = palletDal.GetFirst(t => t.PalletBarcode == palletBarcode);
            if (pallet == null)
            {
                msg = "托盘不存在";
                return false;
            }
            //using var tran = Context.Database.BeginTransaction();
            lockCell.Status = 8; //空箱
            lockCell.BusinessType = EmBusinessType.None;  //入库  
            cellRepository.Update(lockCell);

            return true;
        }
    }
}
